package kr.hkit.powerjava.project_cafe;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.BoxLayout;

import java.awt.FlowLayout;

import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.FocusEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import kr.hkit.powerjava.project_cafe.DbUtil;

import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.FocusListener;

public class MainFrame_cafe extends JFrame implements FocusListener, ActionListener {

	private JPanel contentPane;
	private JButton btn_Input;
	
	private JButton btn_Output01;
	private Dialog_Output01 output01;
	private JPanel panel;
	
	private ProductDAO dao;
	private Product_Service insertProd;
	
	private Panel_Input pProdId;
	private Panel_Input pProdName;
	private Panel_Input pProdPrice;
	private Panel_Input pProdSales;
	private Panel_Input pProdMargin;
	private JButton btn_Output02;
	
	private Dialog_Output02 output02;
	
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Connection con = ConnectionFactory.getConnection();
					MainFrame_cafe frame = new MainFrame_cafe(con);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	
	
	
	public MainFrame_cafe(Connection con) {
	
		setTitle("카페 판매실적 계산");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 433, 233);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
		
		panel = new JPanel();
		contentPane.add(panel);
		panel.setLayout(new GridLayout(0, 2, 0, 0));
		
		pProdId = new Panel_Input("제품코드 : ");
		pProdId.getTf().addFocusListener(this);
		panel.add(pProdId);

		FlowLayout fl_panel_01 = (FlowLayout) pProdId.getLayout();
		fl_panel_01.setAlignment(FlowLayout.LEFT);
		
		pProdName = new Panel_Input("제 품 명 : ");
		panel.add(pProdName);
		FlowLayout fl_panel_02 = (FlowLayout) pProdName.getLayout();
		fl_panel_02.setAlignment(FlowLayout.LEFT);
		
		pProdPrice = new Panel_Input("제품단가 : ");
		FlowLayout fl_panel_03 = (FlowLayout) pProdPrice.getLayout();
		fl_panel_03.setAlignment(FlowLayout.LEFT);
		contentPane.add(pProdPrice);
		
		pProdSales = new Panel_Input("판매수량 : ");
		FlowLayout fl_panel_04 = (FlowLayout) pProdSales.getLayout();
		fl_panel_04.setAlignment(FlowLayout.LEFT);
		contentPane.add(pProdSales);
		
		pProdMargin= new Panel_Input("마  진  율 : ");
		FlowLayout fl_panel_05 = (FlowLayout) pProdMargin.getLayout();
		fl_panel_05.setAlignment(FlowLayout.LEFT);
		contentPane.add(pProdMargin);
		
		JPanel panel_Btn = new JPanel();
		contentPane.add(panel_Btn);
		
		btn_Input = new JButton("입력");
		btn_Input.addActionListener(this);
		panel_Btn.add(btn_Input);
		
		btn_Output01 = new JButton("출력1");
		btn_Output01.addActionListener(this);
		panel_Btn.add(btn_Output01);
		
		btn_Output02 = new JButton("출력2");
		btn_Output02.addActionListener(this);
		panel_Btn.add(btn_Output02);
		
		dao= new ProductDAO(con);
		
	}
	
	private void Disable(){
		pProdId.getTf().setText("");
		pProdName.getTf().setText("");
		pProdPrice.getTf().setText("");
		pProdSales.getTf().setText("");
		pProdMargin.getTf().setText("");
	}
	
	private Product getProdAll() {
		
		String prodId = pProdId.getTf().getText();
		String prodName = pProdName.getTf().getText();
		int prodPrice = Integer.parseInt(pProdPrice.getTf().getText().trim());
		int prodSales = Integer.parseInt(pProdSales.getTf().getText().trim());
		int prodMargin = Integer.parseInt(pProdMargin.getTf().getText().trim());
		
		return new Product(prodId, prodName, prodPrice, prodSales, prodMargin);
		
	}
	
	
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btn_Output02) { //출력2
			actionPerformedBtn_Output02(e);
		}
		if (e.getSource() == btn_Input) { //입력
			actionPerformedBtn_Input(e);
		}
		if (e.getSource() == btn_Output01) { //출력1
			actionPerformedBtn_Output01(e);
		}
	}
	
	protected void actionPerformedBtn_Input(ActionEvent e) { //입력버튼
		
		insertProd = new Product_Service(dao);
		Product p = getProdAll();
		insertProd.insertData(p);
		Disable();
		
	}
	
	protected void actionPerformedBtn_Output01(ActionEvent e) { //출력1
		
		if (output01 == null) {
			output01 = new Dialog_Output01(dao);
		}
		//revalidate();
		output01.setVisible(true);
		
	}


	protected void actionPerformedBtn_Output02(ActionEvent e) {//출력2
		
		if (output02 ==null) {
			output02 = new Dialog_Output02(dao);
		}
		//revalidate();
		output02.setVisible(true);
		
	}
	
	
	/*private void showProdName() { // 포커스아웃
		
		String prodno = pProdId.getTf().getText().trim();
		Product p = new Product();
		p.setProdId(prodno);
		
		try {
			p = dao.searchItems(p);
			pProdName.setTf(p.getProdName());
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}*/
	
	
	public void focusGained(FocusEvent e) {
	}
	
	public void focusLost(FocusEvent e) { //포커스 아웃
		/*if (e.getSource() == pProdId.getTf()) {
			showProdName();
		}*/
	}
	
}
